using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Insight21.Policy;

namespace PHSRAG.Insight21.ClinicalTrials
{
    /// <summary>
    /// Provides for setting up "other costs" associated with a trial budget
    /// </summary>
    public partial class BudgetOtherCosts : NoCachePage
    {
        private Budget budget;
        protected int random;

        #region Event Handlers

        protected void OnPageLoad(object sender, EventArgs e)
        {
            Random r = new Random();
            random = r.Next();

            if (!Page.IsPostBack)
            {
                InitializeControls();
            }
        }

        #endregion

        #region Private Methods

        private void InitializeControls()
        {
            breadCrumb.Add(
                "Work In Progress",
                "BudgetSetup.aspx", 2);
        }

        private void DisableControls(Control formControl)
        {
            switch (formControl.GetType().ToString())
            {
                case "System.Web.UI.WebControls.TextBox":
                    (formControl as TextBox).Enabled = false;
                    break;
                case "System.Web.UI.WebControls.DropDownList":
                    (formControl as DropDownList).Enabled = false;
                    break;
                case "System.Web.UI.WebControls.CheckBox":
                    (formControl as CheckBox).Enabled = false;
                    break;
                case "System.Web.UI.WebControls.CheckBoxList":
                    (formControl as CheckBoxList).Enabled = false;
                    break;
                case "System.Web.UI.WebControls.RadioButton":
                    (formControl as RadioButton).Enabled = false;
                    break;
                case "System.Web.UI.WebControls.RadioButtonList":
                    (formControl as RadioButtonList).Enabled = false;
                    break;
                case "System.Web.UI.WebControls.Button":
                    if ((formControl.ID == "setup") || (formControl.ID == "staffing")
                        || (formControl.ID == "visits") || (formControl.ID == "otherCosts")
                            || (formControl.ID == "summary"))
                    {
                        (formControl as Button).Enabled = true;
                    }
                    else
                    {
                        (formControl as Button).Enabled = false;
                    }
                    break;
                default:
                    foreach (Control childControl in formControl.Controls)
                    {
                        DisableControls(childControl);
                    }
                    break;
            }
        }

        #endregion

        #region Overrides

        /// <summary>
        /// Used to disable controls based on budget status
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            HtmlForm budgetSetupForm = (HtmlForm) Page.FindControl("BudgetOtherCostsForm");
            budget = (Budget) ((ICachePolicy) Session[Names.CachePolicyKey]).GetCachedObject(Budget.CacheKey);

            int accessLevel = budget.AccessLevel;
            string statusCode = budget.StatusCode;

            if (statusCode == ClinicalTrialsConstants.BudgetStatusFinal)
            {
                foreach (Control formControl in budgetSetupForm.Controls)
                {
                    DisableControls(formControl);
                }
            }

            if (!CTAccessHelper.IsUserAdministrator(accessLevel))
            {
                if ((statusCode == ClinicalTrialsConstants.BudgetStatusInProgress) || (statusCode == ClinicalTrialsConstants.BudgetStatusAmended))
                {
                    if (CTAccessHelper.IsUserModifyPending(accessLevel) == false)
                    {
                        foreach (Control formControl in budgetSetupForm.Controls)
                        {
                            DisableControls(formControl);
                        }
                    }
                }
            }
        }

        #endregion

        #region Web Form Designer generated code

        ///<summary>Initializes components used by the page.</summary>
        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.OnPageLoad);
        }

        #endregion
    }
}